package org.ecype.diego.demo;

import static androidx.persistence.CascadeType.REMOVE;
import static androidx.persistence.ColumnType.INT;
import static androidx.persistence.ColumnType.LONG;
import static androidx.persistence.ColumnType.SHORT;
import static androidx.persistence.ColumnType.STRING;
import static org.ecype.diego.DefaultValue.NEVER;
import static org.ecype.diego.DefaultValue.ZERO;

import org.ecype.diego.DefaultValue;

import android.net.Uri;
import androidx.persistence.Column;
import androidx.persistence.Contract;
import androidx.persistence.Entity;
import androidx.persistence.GeneratedValue;
import androidx.persistence.Id;
import androidx.persistence.ManyToOne;
import androidx.persistence.OneToMany;
import androidx.persistence.OneToOne;
import androidx.persistence.access.Incrementer;
import androidx.persistence.access.Marker;
import androidx.persistence.access.Subdirectory;
import androidx.persistence.access.Access;

@Contract(authority = "demo")
public class DemoContract {

	@Entity
	public static class Question {

		@Id
		@GeneratedValue
		@Column(type = INT)
		public static final String ID = Id.COLUMN_NAME;

		@Column(type = LONG, nullable = false)
		public static final String DATE = "_date";

		@Column(type = SHORT, nullable = false)
		@DefaultValue(QuestionState.NEW + "")
		public static final String STATE = "_state";

		@Column(type = STRING)
		public static final String TEXT = "_text";

		@Column(type = INT)
		public static final String PRICE = "_price";

		@OneToMany(targetEntity = Answer.class)
		public static final String ANSWERS = "answers";

		@ManyToOne(targetEntity = Topic.class, optional = true)
		public static final String TOPIC = "topic";

		public static final Uri URI = Uri.parse("content://demo/question");

		@Subdirectory(where = STATE + " = " + QuestionState.ACTIVE)
		public static final Uri URI_ACTIVE = Uri.parse("content://demo/question/active");

		@Access(processor = PendingQuestionProcessor.class)
		public static final Uri URI_PENDING = Uri.parse("content://demo/question/pending");

		@Marker(columns = Question.STATE, values = "" + QuestionState.CLOSED)
		public static final String ACTION_MARK_AS_CLOSED = "mark-as-closed";

		@Incrementer(column = Question.PRICE)
		public static final String ACTION_RAISE_PRICE = "raise-price";

		@OneToOne(targetEntity = Track.class)
		public static final String TRACK = "track";

	}

	public interface QuestionState {
		int PENDING = 0, NEW = 1, ACTIVE = 2, CLOSED = 3;
	}

	@Entity
	public static class Topic {

		@Id
		@GeneratedValue
		@Column(type = INT)
		public static final String ID = Id.COLUMN_NAME;

		@Column(type = STRING, nullable = false)
		public static final String NAME = "_name";

		public static final Uri URI = Uri.parse("content://demo/topic");

	}

	@Entity
	public static class Answer {

		@Id
		@GeneratedValue
		@Column(type = INT)
		public static final String ID = Id.COLUMN_NAME;

		@Column(type = LONG, nullable = false)
		@DefaultValue(NEVER)
		public static final String DATE = "_date";

		@Column(type = STRING)
		public static final String TEXT = "_text";

		@ManyToOne(targetEntity = Question.class, cascade = REMOVE)
		public static final String QUESTION = "question";

	}

	@Entity
	public static class Track extends Attachment {

		@Id
		@GeneratedValue
		@Column(type = INT)
		public static final String ID = Id.COLUMN_NAME;

		public static final Uri URI = Uri.parse("content://demo/track");

	}

	public static class Attachment {

		public static interface AttachmentState {
			int PENDING = 0, DELETED = 1, CREATED = 2, SYNCHED = 3, MODIFIED = 4;
		}

		@Column(type = SHORT, nullable = false)
		public static final String STATE = "_state";

		@Column(type = SHORT, nullable = false)
		@DefaultValue(ZERO)
		public static final String PROGRESS = "_progress";

		@Column(type = LONG, nullable = false)
		@DefaultValue(NEVER)
		public static final String LAST_ACCESS_DATE = "_last_access_date";

		@Column(type = STRING)
		public static final String REMOTE_PATH = "_remote_path";

		@Column(type = STRING)
		public static final String LOCAL_PATH = "_local_path";

	}

}
